Public Class FormExperience

    Dim myExperience As DataTable
    Public i As Integer
    Private Sub initalizeData()
        Try
            loadData()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Public Sub loadData()
        'Get Data From Database
        Try
            myExperience = Experience.GetExperience
            dgvExperience.DataSource = myExperience
        Catch ex As Exception

        End Try
    End Sub

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Dbinterface.OpenConnection()
        initalizeData()
    End Sub

    Private Sub FormExperience_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        UI.FormatDataGrid(dgvExperience)
        btnAdd.Enabled = App.IsAuthorized(Permissions.Manage_Experience_Add)
        btnUpdate.Enabled = App.IsAuthorized(Permissions.Manage_Experience_Edit)
        btnDelete.Enabled = App.IsAuthorized(Permissions.Manage_Experience_Delete)
    End Sub

    Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
        UpdateExperience()
    End Sub
    Public Sub UpdateExperience()


        'pass values from DataGridView to FormEmployeeAED when click Button Detail
        Try


            Dim row = Me.dgvExperience.CurrentRow

            With FormExperienceAED

                If row.Cells("EXPERIENCE_ID").Value Is Nothing Then
                    Exit Sub
                Else
                    .expId = row.Cells("EXPERIENCE_ID").Value
                    .cboEmp_Name.Text = row.Cells("EMPLOYEE_NAME").Value

                    .txtCompanyName.Text = row.Cells("COMPANY_NAME").Value
                    .txtDuration.Text = row.Cells("DURATION").Value
                    .txtPosition.Text = row.Cells("EMPLOYEE_POSITION").Value
                    .txtSalary.Text = row.Cells("SALARY").Value
                    .txtJobDecription.Text = row.Cells("JOB_DESCRIPTION").Value
                    .txtNote.Text = row.Cells("NOTE").Value
                    .txtStartDate.Text = row.Cells("START_DATE").Value
                    .txtEndDate.Text = row.Cells("END_DATE").Value

                    .ShowDialog()
                End If

            End With
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        'Get last DataGridView Rows when click button add
        i = dgvExperience.Rows.Count.ToString
        i = i - 1
        dgvExperience.CurrentCell = dgvExperience.Rows(i).Cells(1)
        Dim frm As New FormExperienceAED
        frm.ShowDialog()
    End Sub

    Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
        Try
            'Check database connection
            Dim cn = Dbinterface.Connection
            If cn.State = ConnectionState.Closed Then

                Exit Sub

            End If

        Catch ex As Exception

        End Try
        Try

            FormDelete.ShowDialog()
            If FormDelete.DialogResult = Windows.Forms.DialogResult.Cancel Then
                Exit Sub
            End If

            'Remove DataGridView Row and Delete data in Database
            For Each row As DataGridViewRow In dgvExperience.SelectedRows
                If row.Cells("EXPERIENCE_ID").Value = Nothing Then

                    Exit Sub

                End If

                Dim expr As New ExperinceInfo
                expr.ExperinceId = row.Cells("EXPERIENCE_ID").Value

                Experience.DeleteExperience(expr)

                Me.dgvExperience.Rows.Remove(row)
                dgvExperience.CurrentCell = dgvExperience.Rows(0).Cells(1)


            Next


        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub dgvExperience_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvExperience.CellContentClick

    End Sub
End Class